﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.WebUtility;
using Garlic.CC.BaseClass;
using Garlic.BLL;
using Garlic.DM.DataModel;
using Garlic.CC.Utility;

public partial class SystemManagement_UserList : BasePage
{
    const int ADMIN_ID = 1;
    UserManager UserMgr = null;//= new UserManager(this.CurrentUser);

    #region page load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //WorkshopMgr = new WorkshopManager(this.CurrentUser);
            if (!Page.IsPostBack)
            {
                //Init Page
                //   InitSearchBar();

                //this.Search();
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    #endregion

    #region method
    /// <summary>
    /// 初始化页面
    /// </summary>
    private void InitPage()
    {

    }

    /// <summary>
    /// 初始化查询栏
    /// </summary>
    private void InitSearchBar()
    {
        //初始化查询列表
        // this.GeneralSearchBar1.ListSourcXYbles = WorkshopMgr.GetSearchBarDataSourcXYbles();


    }

    /// <summary>
    /// 当页面重新刷新后，需要调用search方法进行重新查询
    /// </summary>
    protected override void OnReFreshPostBack()
    {
        Search();
    }

    private void DeleteRecords()
    {
        string checkedRecords = "";
        for (int i = 0; i < this.SmartGrid.Rows.Count; i++)
        {
            CheckBox cbSelected = this.SmartGrid.Rows[i].FindControl("checkitem") as CheckBox;
            if (cbSelected != null && cbSelected.Checked)
            {
                checkedRecords += SmartGrid.DataKeys[i].Value + ",";
            }
        }
        if (string.IsNullOrEmpty(checkedRecords)) return;
        checkedRecords = checkedRecords.Substring(0, checkedRecords.Length - 1);

        UserManager.DeleteMultRecords(checkedRecords);
        this.ShowMessage("删除");
        this.Search();
    }

    private void Search()
    {
        string condition = this.GeneralSearchBar.GetConditionStr();
        this.SmartGrid.DataSourceID = this.SmartDataViewSource.ID;
        //string condition = "";

        //if (searchUserName.Text.Trim() != "")
        //    condition += " user_name like '%" + searchUserName.Text + "%'";
        //if (searchEmployeeName.Text.Trim() != "")
        //{
        //    if (condition != "")
        //        condition += " and ";
        //    condition += " employee_name like '%" + searchEmployeeName.Text + "%'";
        //}
        if (this.CurrentUser.UserID != ADMIN_ID)
        {
            if (!string.IsNullOrEmpty(condition))
            {
                condition += " AND ";
            }
            condition += " user_id<>" + ADMIN_ID.ToString();
        }
        this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = condition;
    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }

    private void ExportExcel()
    {
        string filename = "user";
        string message;
        bool isSuccess = Garlic.Infrustructure.Utilities.WebUtil.Export(false, SmartGrid, ref filename, out message);
        if (isSuccess)
            OutputFilePath = filename;
        else
            Message = message;

    }


    #endregion

    #region toolbar event handler
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        switch (args.Command)
        {
            case ToolbarCommand.Delete:
                DeleteRecords();
                break;
            case ToolbarCommand.Search:
                Search();
                this.divSearch.Visible = false;
                break;
            case ToolbarCommand.HideOrExpand:
                this.divSearch.Visible = !this.divSearch.Visible;
                break;
            case ToolbarCommand.Export:
                ExportExcel();
                break;
        }

    }
    #endregion


    protected void SmartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //if (e.Row.Cells[18].Text == "0")
            //    e.Row.Cells[18].Text = "启用";
            //else if (e.Row.Cells[18].Text == "1")
            //    e.Row.Cells[18].Text = "未启用";
            //else
            //    e.Row.Cells[18].Text = "";

            //switch (e.Row.Cells[17].Text)
            //{
            //    case "0":
            //        e.Row.Cells[17].Text = "JITDD";
            //        break;
            //    case "1":
            //        e.Row.Cells[17].Text = "EPS";
            //        break;
            //    case "2":
            //        e.Row.Cells[17].Text = "RDC";
            //        break;
            //    case "3":
            //        e.Row.Cells[17].Text = "Supplier";
            //        break;
            //    default:
            //        e.Row.Cells[17].Text = "";
            //        break;
            //}

            //switch (e.Row.Cells[16].Text)
            //{
            //    case "1":
            //        e.Row.Cells[16].Text = "A班";
            //        break;
            //    case "2":
            //        e.Row.Cells[16].Text = "B班";
            //        break;
            //    case "3":
            //        e.Row.Cells[16].Text = "C班";
            //        break;
            //    default:
            //        e.Row.Cells[16].Text = "";
            //        break;
            //}
        }
    }
    protected void SmartGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandArgument != null)
        {
            try
            {
                int userID = int.Parse(e.CommandArgument.ToString());

                if (e.CommandName == "InitPass")
                {
                    UserInfo user = UserManager.GetInfo(userID);
                    string password;
                    if (user.User_type == 0)
                        password = txtJITDDPassword.Text;
                    else if (user.User_type == 1)
                        password = txtEPSPassword.Text;
                    else if (user.User_type == 2)
                        password = txtRDCPassword.Text;
                    else if (user.User_type == 3)
                        password = txtSupplierPassword.Text;
                    else
                        password = "Pass1234";
                    UserManager.InitPass(userID, password);
                }
                else if (e.CommandName == "UnLock")
                {
                    UserManager.UnLock(userID);

                    #region Added by 冯友军， 更新解锁后的页面显示状态
                    this.SmartGrid.DataBind();
                    #endregion
                }
            }
            catch
            {
            }
        }
    }
}
